package netTracker;

import java.io.Serializable;
import java.util.*;

/**
 * Represents a cell intended for use within a tracker. Every cell contains a
 * note and a velocity. A cell can be outlined or selected.
 * 
 * @author David Cummings
 */

public class Cell implements Serializable{
	
	private static final long serialVersionUID = 1L;

	/** The note represented within the cell. */
	Note note;

	/** The velocity represented within the cell. */
	int velocity;

	/** Represents whether the cell is selected (by the current user.) */
	boolean selected;
	
	/** Represents whether the cell is highlighted (by playback.) */
	boolean highlighted;

	/** HashMap of booleans that indicate whether the cell is outlined by other users. 
	 * 	Key, Value is Owner ID, True/False */
	HashMap<Integer, Boolean> outlined;
	
	/** Coordinates of the cell within a pattern. */
	int cellX, cellY;

	/**
	 * Constructs a cell with an empty note.
	 */
	public Cell(){
		outlined = new HashMap<Integer, Boolean>();
		note = new Note(Note.EMPTY);
	}
	
	/**
	 * Constructs a cell with a specific note.
	 */
	public Cell(double frequency){
		outlined = new HashMap<Integer, Boolean>();
		note = new Note(frequency);
	}
	
	/**
	 * @param note the note to set.
	 */
	public void setNote(Note note){
		this.note = note;
	}

	/**
	 * @return the note in the cell.
	 */
	public Note getNote(){
		return note;
	}

	/**
	 * @return the velocity
	 */
	public int getVelocity() {
		return velocity;
	}

	/**
	 * @param velocity
	 *            the velocity to set
	 */
	public void setVelocity(int velocity) {
		this.velocity = velocity;
	}

	/**
	 * @return whether the cell is currently selected (by the current user)
	 */
	public synchronized boolean isSelected() {
		return selected;
	}

	/**
	 * @param selected
	 *            the value to assign to selected
	 */
	public synchronized void setSelected(boolean selected) {
		this.selected = selected;
	}

	/**
	 * @return whether the cell is outlined by a given user
	 * @param the ID of the user we are checking
	 */
	public synchronized boolean isOutlined(int outlineID) {
		return outlined.get(outlineID);
	}

	/**
	 * @return whether the cell is outlined by any user at all
	 */
	public synchronized boolean isOutlined() {
		if(outlined.values().contains(true))
			return true;
		else
			return false;
	}

	/**
	 * @param outlineID the ID of the node outlining this cell
	 * @param isOutlined the value to assign to outlined
	 */
	public synchronized void setOutlined(int outlineID, boolean isOutlined) {
		outlined.put(outlineID, isOutlined);
	}
	
	/** @return a string representation of the note in this cell. */
	public String toString(){
		return note.toString();
	}

	public int getCellX() {
		return cellX;
	}
	
	public int getCellY(){
		return cellY;
	}

	/** Sets the note in a cell based on a string. */
	public void setNoteName(String noteName) {
		note.setNoteName(noteName);
	}

	public String getNoteName() {
		return note.toString();
	}

	public void setHighlighted(boolean highlighted) {
		this.highlighted = highlighted;
	}
	
	public boolean isHighlighted(){
		return highlighted;
	}
}
